1. Field of the Invention
The present invention generally relates to a method and system for transplanting source code, and more particularly to a method and system for automated code refactoring.
2. Description of the Related Art
In order to same time, many developers perform small-scale code reuse tasks. That is, many developers adopt existing source code, and integrate that existing source code or a fragment thereof into a new application. Generally, the existing source code expresses some required function, but many details must be manually changed in order to properly integrate the existing source code into the target source code of the new application.
FIG. 1 shows an example of how a conventional code reuse task is performed. Specifically, to adopt existing source code 120, generally a developer will copy the relevant fragment 130 of the existing source code 120 and then paste that fragment 130 into the target source code 110. After the developer pastes the fragment 130 of the existing source code 120 into the target source code 110, the developer is then required to perform a burdensome task of going through the fragment 130 and updating it so that it is able to be integrated with the target source code 110 of the new application.
A significant challenge of reusing code is identifying and correctly updating numerous change points, which are required to adapt existing code to the new application. Specifically, a given piece of source code may implement an algorithm or method, which has a general utility. Incorporating this piece of source code in another application typically requires the developer to manually locate and update change points between the reused piece of source code and the target application. For example, the developer may have to manually locate and update change points including, for example, type definitions, variable names, method names, and signatures.
Depending on the structure of the fragment of the existing code to be adapted, identifying at least the aforementioned change points may require a detailed understanding of not only how the reused piece of source code was used in the previous application, but also of how the reused piece of source code will interact with the target application. This level of detailed understanding may require substantial time and intellectual effort. Therefore, due to the small scale of such tasks, the actual time required, even for a relatively small-scale code reuse task, can quickly outweigh any perceived time saving benefit.
As is shown in FIG. 2, some development environments allow the developer to partially streamline this process. Specifically, in some integrated development environments (IDEs) a developer can manually select an individual token of a specific change point, e.g., “loadDocModel,” and the IDE will automatically update all instances of code that invoke the function “loadDocModel” based on a single manual user input that renames this particular token. This process is commonly referred to as refactoring.
The automatic refactoring provided by IDEs is generally known to be more efficient than manual identification and individual updating of each token for each specific change point. However, even with an IDE that uses refactoring, the IDE is not aware that the transplanted code has been copied and pasted into the target source code. Instead, in order to successfully transplant the existing source code into the target source of the new application, the developer must still manually identify the portions of the existing code that need to be changed and then update the identified portions of the existing source code.
Therefore, while automatic code refactoring can assist in the identification of the change points, the developer is still required to select the tokens to be changed and then enter replacement values for those tokens. As there may be many change points, this process is quite laborious and prone to error.
As a result, the present inventors have recognized that there is a need for a way to automatically identify portions of existing code which need to be updated, as well as a need for a way of suggesting updates to the existing code when it is integrated into the target application.